package com.yss.job.domain;

import java.util.Date;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yss.common.core.annotation.EasyExcel;
import com.yss.common.core.utils.easyexecl.IntegerConverter;
import com.yss.common.core.utils.easyexecl.StringConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yss.common.core.constant.ScheduleConstants;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.domain.BaseEntity;
import com.yss.job.util.CronUtils;

/**
 * 定时任务调度表 sys_job
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("sys_job")
public class SysJob extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 任务ID
     */
    @ExcelIgnore
    @TableId("ID")
    private String id;

    /**
     * 任务名称
     */
    @NotBlank(message = "任务名称不能为空")
    @Size(min = 0, max = 64, message = "任务名称不能超过64个字符")
    @ExcelProperty(value = "任务名称")
    @TableField("JOB_NAME")
    private String jobName;

    /**
     * 任务组名
     */
    @ExcelProperty(value = "任务组名")
    @TableField("JOB_GROUP")
    private String jobGroup;

    /**
     * 调用目标字符串
     */
    @NotBlank(message = "调用目标字符串不能为空")
    @Size(min = 0, max = 500, message = "调用目标字符串长度不能超过500个字符")
    @ExcelProperty(value = "调用目标字符串")
    @TableField("INVOKE_TARGET")
    private String invokeTarget;

    /**
     * cron执行表达式
     */
    @NotBlank(message = "Cron执行表达式不能为空")
    @Size(min = 0, max = 255, message = "Cron执行表达式不能超过255个字符")
    @ExcelProperty(value = "执行表达式 ")
    @TableField("CRON_EXPRESSION")
    private String cronExpression;

    /**
     * cron计划策略
     */
    @ExcelProperty(value = "计划策略 ", converter = StringConverter.class)
    @EasyExcel(readConverterExp = "0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行")
    @TableField("MISFIRE_POLICY")
    private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

    /**
     * 是否并发执行（0允许 1禁止）
     */
    @ExcelProperty(value = "并发执行", converter = StringConverter.class)
    @EasyExcel(readConverterExp = "0=允许,1=禁止")
    @TableField("CONCURRENT")
    private String concurrent;

    /**
     * 任务状态（1正常 2暂停）
     */
    @ExcelProperty(value = "任务状态", converter = IntegerConverter.class)
    @EasyExcel(readConverterExp = "1=正常,2=暂停")
    @TableField("STATUS")
    private Integer status;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getNextValidTime() {
        if (StringUtils.isNotEmpty(cronExpression)) {
            return CronUtils.getNextExecution(cronExpression);
        }
        return null;
    }


}
